from django.http import HttpResponse,HttpResponseRedirect
from django.template.loader import get_template
from django.template import RequestContext

from Molex.node.models import Post,Node,Comment

def index(request):
    t=get_template('index.html')
    posts=Post.objects.all()
    c=RequestContext(request,{'posts':posts})
    html=t.render(c)
    
    return HttpResponse(html)

def post(request,post_id):
    if request.method=='GET':
        t=get_template('post.html')
        post=Post.objects.get(id=post_id)
        comments=post.comment_set.all()
        node=post.node
        c=RequestContext(request,{'post':post,
                                  'comments':comments,
                                  'node':node,})
        html=t.render(c)
        
        return HttpResponse(html)
    
    if request.method=='POST':
        if 'content' in request.POST:
            comment=Comment(author=request.user,post_id=post_id,content=request.POST['content'])
            comment.save()
        return HttpResponseRedirect('')

def node(request,node_id):
    t=get_template('node.html')
    posts=Post.objects.filter(node=node_id)
    node=Node.objects.get(id=node_id)
    c=RequestContext(request,{'posts':posts,
                              'node':node,})
    html=t.render(c)
    
    return HttpResponse(html)
    